Page({
  data: {
    openid:'',
    system:'',
    brand:'',
    model:'',
    latitude:'',
    longitude:'',
    speed:'',
    accuracy:'',
    altitude:'',
    horizontalAccuracy:'',
    name:'',
  },
  onLoad: function (options) {
    console.log(options)
    this.login();
    // this.get_tel();
    // this.get_lbs();
  },
  login: function () {
    console.log('logining..........');
    const that = this;

    //调用登录接口
    wx.login({
      success: function (e) {
        var openid = wx.getStorageSync('openid');
        if (!openid) {
          wx.request({
            url: 'https://2016.bookgo.com.cn/wxapp/index/login',
            data: {
              code: e.code
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log('获取openid');
              console.log(res.data)
              wx.setStorageSync('openid', res.data.openid)
              that.setData({
                openid: res.data.openid,
                loginurl: 'https://2016.bookgo.com.cn/wxapp/index/test_web_url/openid/' + res.data.openid

              });
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }else{
          that.setData({
            openid: openid
          });
        } 
        console.log('wxlogin successd........');
      }
    });
  },
  get_tel:function(){
    const _this = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.brand)
        console.log(res.model)
        console.log(res.system)
        _this.setData({
          brand:res.brand,
          model:res.model,
          system:res.system
        });
      }
    })
  },
  get_lbs:function(){
    const _this = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        var altitude = res.altitude
        var horizontalAccuracy = res.horizontalAccuracy
        _this.setData({
          latitude : latitude,
          longitude : longitude,
          speed : speed,
          accuracy : accuracy,
          altitude : altitude,
          horizontalAccuracy : horizontalAccuracy
        });
      }
    });
    setTimeout(function(){
      _this.get_lbs();
    },3000);
  },
  push_lbs:function(){
    const _this = this;
    if(!_this.data.name) {
      wx.showModal({
        title: '提示',
        content: '必须填入地点名称',
        showCancel:false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      })
    }
    wx.showLoading({
      title: '提交中',
    })
    wx.request({
      url: 'https://2016.bookgo.com.cn/wxapp/seatnum/push_lbs',
      method:'POST',
      data:{
        openid:_this.data.openid,
        device:_this.data.brand,
        model:_this.data.model,
        system:_this.data.system,
        latitude: _this.data.latitude,
        longitude: _this.data.longitude,
        accuracy: _this.data.horizontalAccuracy,
        name:_this.data.name
      },
      success: function (res) {
        console.log(res.data)
        setTimeout(function () {
          wx.hideLoading()
        }, 1000)
      }
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  push_message:function(e){
    // console.log(e);return false;
    var formId = e.detail.formId;
    console.log(formId);
    const that = this;
    wx.getUserInfo({
      success: function (res) {
        console.log(res.userInfo);
        var name = res.userInfo.nickName;
        wx.request({
          url: 'https://2016.bookgo.com.cn/wxapp/user/send_test_template',
          method:'POST',
          data:{
            openid:that.data.openid,
            name:name,
            form_id:formId
          },
          success:function(res){

          }
        })
      }
    })
  }

})
